package com.loong.musicdemo.ui.find.activity.ui.detail.fragment;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.loong.musicdemo.R;
import com.loong.musicdemo.model.Album;
import com.loong.musicdemo.model.Singer;
import com.loong.musicdemo.ui.find.activity.ShowAlbumPlayListActivity;
import com.loong.musicdemo.util.dataDeal.SingerDeal;

import java.util.ArrayList;
import java.util.List;

public class SingerAlbumFragment extends Fragment {
    private Singer singer;
    private AlbumAdapter albumAdapter;
    List<Album> albumList = new ArrayList<>();

    public SingerAlbumFragment(Singer singer) {
        // Required empty public constructor
        this.singer = singer;
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     * @return A new instance of fragment SingerAlbumFragment.
     */
    public static SingerAlbumFragment newInstance(Singer singer) {
        SingerAlbumFragment fragment = new SingerAlbumFragment(singer);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_singer_album, container, false);
        RecyclerView singer_album_recycler_view = root.findViewById(R.id.singer_album_recycler_view);
        albumAdapter = new AlbumAdapter(albumList);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        singer_album_recycler_view.setLayoutManager(layoutManager);
        singer_album_recycler_view.setAdapter(albumAdapter);
        new AlbumTask().execute();
        return root;
    }

    class AlbumTask extends AsyncTask<Singer,Void,List<Album>>{
        @Override
        protected void onPostExecute(List<Album> albums) {
            super.onPostExecute(albums);
            albumList.clear();
            albumList.addAll(albums);
            albumAdapter.notifyDataSetChanged();
        }

        @Override
        protected List<Album> doInBackground(Singer... singers) {
            return SingerDeal.getSingerAlbumIds(singer.getId());
        }
    }

    class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.ViewHolder>{
        private List<Album> albumList;
        private RequestOptions requestOptions;

        public AlbumAdapter(List<Album> albumList) {
            this.albumList = albumList;
            requestOptions = new RequestOptions();
            requestOptions.placeholder(R.drawable.temp);
            requestOptions.circleCropTransform();
            requestOptions.bitmapTransform( new RoundedCorners(6));
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.singer_album_item,parent,false);
            ViewHolder holder = new ViewHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            Album album = albumList.get(position);
            Glide.with(getContext()).load(album.getAlbumPicUrl()).apply(requestOptions).skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .into(holder.album_img);
            holder.album_name.setText(album.getAlbumName());
            holder.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Album a = albumList.get(holder.getAdapterPosition());
                    Intent albumIntent = new Intent(getContext(), ShowAlbumPlayListActivity.class);
                    albumIntent.putExtra("albumName", a.getAlbumName());
                    albumIntent.putExtra("albumImg", a.getAlbumPicUrl());
                    albumIntent.putExtra("albumIDs", (ArrayList) a.getSongIdsInAlbum());
                    albumIntent.putExtra("albumCreatorName", singer.getName());
                    albumIntent.putExtra("albumCreatorPic", singer.getImg1v1Url());
                    albumIntent.putExtra("albumCommentCount", a.getAlbumCommentCount());
                    albumIntent.putExtra("albumPlayCount", "0");
                    albumIntent.putExtra("albumId", a.getAlbumId());
                    getContext().startActivity(albumIntent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return albumList.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            ImageView album_img;
            TextView album_name;
            View view;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                view = itemView;
                album_img = itemView.findViewById(R.id.album_img);
                album_name = itemView.findViewById(R.id.album_name);
            }
        }
    }
}